#!/user/bin/env python
# -*- coding:utf-8 -*-
#filename:controllers/tfeetCommentsjson.py

'''
Created on 2011-8-30

@author: zc

modify on 2011-09-12  None json--> {}

'''
from dbhandle import mysqldb
from config import sqlmap
from config.settings import NEXTFEET
from controllers.utils import logger
logger = logger.logger()


class tfeetCommentsjson():
    '''    classdocs    '''
    __dbo = mysqldb.mysqldb()

    
    def tfeetCommentsjson( self, tfid = 0, cid = 0 ):
        """ 根据tifd获取评论内容并且组合json数据返回 """
        comments = None
        sql_comments = ""
        
        if tfid <> 0:
            if cid == 0:
                sql_comments = sqlmap.SQL_GETCOMMENTS1ST % ( tfid, NEXTFEET )  
            elif cid > 0:
                sql_comments = sqlmap.SQL_GETCOMMENTSMANY % ( tfid, cid, NEXTFEET )  
        
        if len( sql_comments ) > 0:
            comments_res = self.__dbo.query( sql_comments )
        
        if comments_res:
            comments = '{"comments":['
            for cts in comments_res:
                comments = comments + ( '''{
                              "uname" : "''' + cts.uname + '''",
                              "uid" : "''' + str( cts.uid ) + '''",
                              "cid" : "''' + str( cts.cid ) + '''",
                              "comment": "''' + cts.comment.replace( '\n', '' ).replace( '\r', '' ) + '''",
                              "datetime" : "''' + str( cts.date ) + '''"
                    },''' )
                #print comments
            comments = comments[:-1] #去掉最后一个逗号
            
            comments = comments + "]}"
        else:
            comments = "{}"
            
        return comments



if __name__ == "__main__":
    """ 测试 """
    t = tfeetCommentsjson()
    s = t.tfeetCommentsjson( 274 )
    print s
    

        
